package acceptTests;

import java.util.Iterator;
import sessionManager.SessionManager;
import sessionManager.SessionManagerIF;
import user.UserIF;
import user.blog.BlogIF;
import user.post.Post;


public class US08Facade {
	private SessionManagerIF session;
	
	public US08Facade() {
		session = SessionManager.getInstance();
	}
	
	public void loadData() throws Exception {
		session.loadData();
	}
	
	public void createProfile(String login, String senha, String nome_exibicao,
			String email, String sexo, String dataNasc, String endereco,
			String interesses, String quem_sou_eu, String filmes,
			String musicas, String livros) throws Exception {
		session.createUser(login, senha, nome_exibicao, email, sexo, dataNasc, endereco, interesses, quem_sou_eu, filmes, musicas, livros);
	
	}
	
	public String logon(String login, String password) throws Exception {
		return session.loginUser(login, password);
	}
	
	public String getBlogByLogin(String login, Integer index) throws Exception {
		return session.getUserByLogin(login).getBlogs().get(index).getId();
		
	}
	
	public String getPost(String blogId, Integer index) throws Exception {
		Iterator<UserIF> itUsers = session.getUsers().iterator();
		boolean foundIt = false;
		BlogIF blog = null;
		while (itUsers.hasNext() && !foundIt) {
			UserIF tmpUser = itUsers.next();
			try {
				blog = tmpUser.getBlog(blogId);
				foundIt = true;
			} catch (Exception e) {
				// Nothing here.
			}
		}
		return blog.getPostManager().getPost(index).getId();
	}
	
	public String addComment(String sessionId, String postId, String text) throws Exception {
		Iterator<UserIF> itUsers = session.getUsers().iterator();
		
		while (itUsers.hasNext()) {
			UserIF tmpUser = itUsers.next();
			Iterator<BlogIF> itBlog = tmpUser.getBlogs().iterator();
			while(itBlog.hasNext()) {
				BlogIF tmpBlog = itBlog.next();
				if(tmpBlog.getPostManager().find(postId) != null) {
					return tmpBlog.getPostManager().find(postId).addComment(
							session.getUser(sessionId).getLogin(), text);
				}
			}
		}
		return null;
	}
	
	public int getNumberOfComments(String postId) throws Exception {
		Iterator<UserIF> itUsers = session.getUsers().iterator();
		
		while (itUsers.hasNext()) {
			UserIF tmpUser = itUsers.next();
			Iterator<BlogIF> itBlog = tmpUser.getBlogs().iterator();
			while(itBlog.hasNext()) {
				BlogIF tmpBlog = itBlog.next();
				if(tmpBlog.getPostManager().find(postId) != null) {
					return tmpBlog.getPostManager().getCommentManager(postId).getComments().size();
				}
			}
		}
		return -1;
	}
	
	public String getComment(String postId, Integer index) throws Exception {
		Iterator<UserIF> itUsers = session.getUsers().iterator();
		
		while (itUsers.hasNext()) {
			UserIF tmpUser = itUsers.next();
			Iterator<BlogIF> itBlog = tmpUser.getBlogs().iterator();
			while(itBlog.hasNext()) {
				BlogIF tmpBlog = itBlog.next();
				if(tmpBlog.getPostManager().find(postId) != null) {
					return tmpBlog.getPostManager().find(postId)
							.getCommentManager().getComments().get(index)
							.getId();
				}
			}
		}
		return null;
	}
	
	public String getCommentText(String idComment) throws Exception {
		Iterator<UserIF> itUsers = session.getUsers().iterator();
		
		while (itUsers.hasNext()) {
			UserIF tmpUser = itUsers.next();
			Iterator<BlogIF> itBlog = tmpUser.getBlogs().iterator();
			while(itBlog.hasNext()) {
				BlogIF tmpBlog = itBlog.next();
				Iterator<Post> itPosts = tmpBlog.getPostManager().getPosts().iterator();
				while(itPosts.hasNext()) {
					Post tmpPost = itPosts.next();
					if(tmpPost.getCommentManager().findCommentByID(idComment) != null) {
						return tmpPost.getCommentManager().findCommentByID(idComment).getText();
					}
				}
				
			}
		}
		return null;
	}
	
	public void logoff(String sessionId) throws Exception {
		session.logoffUser(sessionId);
	}
	
	public void saveData() throws Exception {
		session.saveData();
	}
	
	public String getCommentAuthor(String idComment) throws Exception {
		Iterator<UserIF> itUsers = session.getUsers().iterator();
		
		while (itUsers.hasNext()) {
			UserIF tmpUser = itUsers.next();
			Iterator<BlogIF> itBlog = tmpUser.getBlogs().iterator();
			while(itBlog.hasNext()) {
				BlogIF tmpBlog = itBlog.next();
				Iterator<Post> itPosts = tmpBlog.getPostManager().getPosts().iterator();
				while(itPosts.hasNext()) {
					Post tmpPost = itPosts.next();
					if(tmpPost.getCommentManager().findCommentByID(idComment) != null) {
						return tmpPost.getCommentManager().findCommentByID(idComment).getAuthor();
					}
				}
				
			}
		}
		return null;
	}
}